﻿using BCCommon;
using BCCommon.TMS.ManageFeeBusiness;
using BCData.TMS.PlatformManageFee.PlatformManageFeeConfig;
using BCDto.Sim.Admin.Admin;
using BCDto.TMS.PlatformManageFee.PlatformManageFeeConfig;
using BCEntity.Common.EntityNameConst;
using BCService.Utils;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BCService.TMS.PlatformManageFee.PlatformManageFeeConfig
{
    public class PlatformManageFeeConfigService : IPlatformManageFeeConfigService
    {
        private IRedisService redisService;
        private readonly IPlatformManageFeeConfigData platformManageFeeConfigData;
        private readonly string key = string.Format("{0}", EntityNameConst.PlatformManageFeeConfigEntity);

        public PlatformManageFeeConfigService(IRedisService redisService, 
            IPlatformManageFeeConfigData platformManageFeeConfigData)
        {
            this.redisService = redisService;
            this.platformManageFeeConfigData = platformManageFeeConfigData;
        }

        public PlatformManageFeeConfigDto Update(AdminDto admin, long platformManageFeeConfigId, PlatformManageFeeConfigPutDto dto)
        {
            var result = this.platformManageFeeConfigData.GetEntity(platformManageFeeConfigId).Result;
            if (result == null)
            {
                throw new ArgumentException("平台管理费配置信息不存在");
            }
            if (admin.CompanyId != 1 && admin.CompanyId != result.CompanyId)
            {
                throw new ArgumentException("您暂无权限修改此配置信息");
            }
            result.ManageFeeRatio = dto.ManageFeeRatio;
            this.redisService.DeleteMultipleKey(key, false);
            return result.As<PlatformManageFeeConfigDto>();
        }

        public void Delete(AdminDto admin, long platformManageFeeConfigId)
        {
            var result = this.platformManageFeeConfigData.GetEntity(platformManageFeeConfigId).Result;
            if (result == null)
            {
                throw new ArgumentException("平台管理费配置信息不存在");
            }
            if (admin.CompanyId != 1 && admin.CompanyId != result.CompanyId)
            {
                throw new ArgumentException("您暂无权限修改此配置信息");
            }
            this.platformManageFeeConfigData.Delete(platformManageFeeConfigId).GetAwaiter().GetResult();
            this.redisService.DeleteMultipleKey(key, false);
        }

        public PlatformManageFeeConfigDto GetById(long platformManageFeeConfigId)
        {
            var result = this.platformManageFeeConfigData.GetEntity(platformManageFeeConfigId).Result;
            return result.As<PlatformManageFeeConfigDto>();
        }

        public PageableList<PlatformManageFeeConfigViewDto> GetList(AdminDto admin, int? companyId, TMSManageFeeBusinessType? manageFeeBusinessType, int pageIndex, int pageSize)
        {
            long? logonCompanyId = admin.CompanyId;
            if (admin.CompanyId == 1)
            {
                logonCompanyId = null;
            }
            string cacheKey = this.redisService.GetKey(key, logonCompanyId, companyId, manageFeeBusinessType, pageIndex, pageSize);
            var result = this.redisService.TryGet(cacheKey, () =>
            {
                var (data, dataCount) = this.platformManageFeeConfigData.GetList(logonCompanyId, companyId, manageFeeBusinessType, pageIndex, pageSize).Result;
                return new PageableList<PlatformManageFeeConfigViewDto>()
                {
                    Count = dataCount,
                    Items = data.As<List<PlatformManageFeeConfigViewDto>>(),
                    PageIndex = pageIndex,
                    PageSize = pageSize
                };
            }, TimeSpan.FromDays(1));
            return result;
        }

        public void UpdateCurrentlyPlanId(long platformManageFeeExecutionPlanId, long subAgentRecordId, TMSManageFeeBusinessType manageFeeBusinessType, decimal manageFeeRatio)
        {
            this.platformManageFeeConfigData.UpdateCurrentlyPlanId(platformManageFeeExecutionPlanId, subAgentRecordId, manageFeeBusinessType, manageFeeRatio).GetAwaiter().GetResult();
            this.redisService.DeleteMultipleKey(key, false);
        }
    }
}
